package GUI;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JTextPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.GridBagConstraints;


public class Principal extends JFrame {
	
	//private ClasePrincipal principal = new ClasePrincipal();  //  @jve:decl-index=0:

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTabbedPane jTabbedPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JButton jButton = null;

	private JTextArea jTextArea = null;

	private JScrollPane jScrollPane = null;

	private JComboBox jComboBox = null;

	private JLabel jLabel = null;
	
	public TextAreaOutputStream taos = TextAreaOutputStream.getInstance(getJTextArea());

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JTextField jTextField = null;

	private JCheckBox jCheckBox = null;

	private JButton jButton3 = null;

	private JButton jButton4 = null;

	private JComboBox jComboBox1 = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JComboBox jComboBox2 = null;

	private JTextField jTextField1 = null;

	private JButton jButton5 = null;

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(23, 175, 313, 269));
			jScrollPane.setViewportView(getJTextArea());
			
			jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
				public void adjustmentValueChanged(AdjustmentEvent e){
				jTextArea.select(jTextArea.getHeight()+100000,0);
				}});


		}
		return jScrollPane;
	}
	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(196, 44, 105, 24));
			jComboBox.setEditable(false);
			jComboBox.addItem("256");
			jComboBox.addItem("512");
			jComboBox.addItem("1024");
			jComboBox.addItem("2048");
			jComboBox.addItem("4096");
		}
		return jComboBox;
	}
	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(105, 9, 99, 21));
			jButton1.setText("Actualizar");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					new ThreadActualizarArbol().start();
				}
			});
		}
		return jButton1;
	}
	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(214, 9, 72, 21));
			jButton2.setText("Listar");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					new ThreadListarArbol().start();
				}
			});
		}
		return jButton2;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(8, 87, 212, 19));
			jTextField.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					//System.out.println("actionPerformed()");
					new ThreadSearchArbol(getJTextField().getText(),getJCheckBox().isSelected()).start();
				
				}
			});
		}
		return jTextField;
	}
	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setBounds(new Rectangle(9, 65, 168, 21));
			jCheckBox.setText("Búsqueda Exacta");
		}
		return jCheckBox;
	}
	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(225, 85, 78, 21));
			jButton3.setText("Buscar");
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					//System.out.println("actionPerformed()");
					new ThreadSearchArbol(getJTextField().getText(),getJCheckBox().isSelected()).start();
				}
			});
		}
		return jButton3;
	}
	/**
	 * This method initializes jButton4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setBounds(new Rectangle(4, 12, 71, 49));
			jButton4.setText("Crear");
			jButton4.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					new ThreadCrearFirmas((String)getJComboBox2().getSelectedItem(),(String)getJComboBox1().getSelectedItem()).start();
				
				}
			});
		}
		return jButton4;
	}
	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			jComboBox1 = new JComboBox();
			jComboBox1.setBounds(new Rectangle(238, 10, 63, 24));
			jComboBox1.addItem("1");
			jComboBox1.addItem("2");
			jComboBox1.addItem("3");
			jComboBox1.addItem("4");
			jComboBox1.addItem("5");
			jComboBox1.addItem("6");
			jComboBox1.addItem("7");
		}
		return jComboBox1;
	}
	/**
	 * This method initializes jComboBox2	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox2() {
		if (jComboBox2 == null) {
			jComboBox2 = new JComboBox();
			jComboBox2.setBounds(new Rectangle(238, 39, 63, 24));
			jComboBox2.addItem("256");
			jComboBox2.addItem("512");
			jComboBox2.addItem("1024");
			jComboBox2.addItem("2048");
			jComboBox2.addItem("4096");
		}
		return jComboBox2;
	}
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(8, 87, 212, 19));
			jTextField1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					new ThreadSearchFirmas(getJTextField1().getText()).start();
				}
			});
		}
		return jTextField1;
	}
	/**
	 * This method initializes jButton5	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton();
			jButton5.setBounds(new Rectangle(225, 85, 78, 21));
			jButton5.setText("Buscar");
			jButton5.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					/////////////System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					clear();
					new ThreadSearchFirmas(getJTextField1().getText()).start();
				}
			});
		}
		return jButton5;
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Principal inst = new Principal();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	/**
	 * This is the default constructor
	 */
	public Principal() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(367, 482);
		this.setContentPane(getJContentPane());
		this.setTitle("Bob Nervio Search Engine");
		this
		.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		       
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJTabbedPane(), null);
			jContentPane.add(getJScrollPane(), null);
		}
		return jContentPane;
	}

	

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setBounds(new Rectangle(25, 14, 313, 147));
			jTabbedPane.addTab("Indice Invertido", null, getJPanel(), null);
			jTabbedPane.addTab("Signature Files", null, getJPanel1(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(5, 47, 175, 15));
			jLabel.setText("Tamaño del nodo en bytes");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJComboBox(), null);
			jPanel.add(jLabel, null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJTextField(), null);
			jPanel.add(getJCheckBox(), null);
			jPanel.add(getJButton3(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(80, 39, 155, 24));
			jLabel2.setText("Tamaño del hash en bits");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(80, 13, 133, 21));
			jLabel1.setText("Funciones de hash");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(getJButton4(), null);
			jPanel1.add(getJComboBox1(), null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getJComboBox2(), null);
			jPanel1.add(getJTextField1(), null);
			jPanel1.add(getJButton5(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(19, 9, 76, 21));
			jButton.setText("Crear");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					clear();
//					System.out.println("PROCESANDO");
//					System.out.println("");
//					System.out.println("Esto puede demorar algunos minutos");
//					System.out.println("durante los cuales el programa no responde");
//					System.out.println("Por favor espere...");
					//principal.CrearArbol((String)getJComboBox().getSelectedItem());
					new ThreadCrearArbol((String)getJComboBox().getSelectedItem()).start();
					
					//System.out.println((String)getJComboBox().getSelectedItem()); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
			

		}
		return jTextArea;
	}
	
	private void clear(){
		jTextArea.setText("");
	}

}  //  @jve:decl-index=0:visual-constraint="76,84"
